'''
Created on Dec 8, 2011

@author: ibrahimelfar
'''

from django.core.management import setup_environ



#from django.conf import settings
#settings.configure(DEBUG=True, TEMPLATE_DEBUG=True,
#                   TEMPLATE_DIRS=('C:/Users/ibrahimelfar/source/dev-tools-that-listen/experimental/prototype'))

from django.template import loader
from django import template
import django
import django.template
from prototype.core import master_registry
import prototype.html


from django.core.management import execute_manager
import imp
try:
    imp.find_module('settings') # Assumed to be in the same directory.
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
    sys.exit(1)

import settings
print settings.TEMPLATE_DIRS

setup_environ(settings)

import os.path

from django.conf import global_settings

register = template.Library()

#fix everywhere log stuff

@register.filter('myfilter')
def myfilter(value, arg):
    return 'hello' + arg

register.filter('myfilter', myfilter)

if __name__ == '__main__':
    language = 'html'
    natural_languages = ['en-us']
    apps = ['notepad', 'eclipse']
    # check if language is registered.
    
    processed_activities = []
    for activity in master_registry.registries[language].activities:
        print 'Processing ' + activity.__name__
        if len(activity.pattern_variables) == 0:
            processed_activities.append(activity)
            continue
        
        # fix - support more than one pattern variable.
        for key in activity.pattern_variables:
            for value in activity.pattern_variables[key]:
                print key + ' = ' + value
                processed_activities.append(activity)
    print len(processed_activities)


#    template_file =  os.path.join(os.path.dirname(__file__), 'commands.xml').replace('\\','/')

    f = open(os.path.join(os.path.dirname(__file__), 'commands.xml').replace('\\','/'), 'r')
    content = f.read()
    f.close()
    
    context = django.template.Context(dict(master_registry.registries[language].context.items() +
                                           
                                           {'apps': apps, 'activities': processed_activities}.items()))
#    print context['variables']
#    print django.template.Template(content).render(context)
#    

    print 'hello'
    print loader.render_to_string('commands.xml', context)
    